using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.AccountsAPI;
using Rensoft.CRM.Client.Editors;
using Rensoft.CRM.Client.Properties;

namespace Rensoft.CRM.Client.ItemViews
{
    public partial class AccountView : BaseView
    {
        private AccountType accountTypes;
        private List<Account> accountList;

        protected Account[] AccountArray
        {
            get { return accountList.ToArray(); }
            set { accountList = new List<Account>(value); }
        }

        protected AccountView()
        {
            InitializeComponent();
        }

        protected AccountView(Program program) : base(program)
        {
            InitializeComponent();
        }

        protected AccountView(Program program, AccountType accountTypes)
            : this(program)
        {
            this.accountTypes = accountTypes;
        }

        protected override void PerformLoad()
        {
            AccountsService service = new AccountsService();
            service.UseDefaultCredentials = true;
            AccountArray = service.AccountFetchByType(accountTypes);
        }

        protected override void LoadComplete(RunWorkerCompletedEventArgs e)
        {
            if (AccountArray != null)
            {
                ItemsListView.VirtualListSize = AccountArray.Length;
                ItemsListView.VirtualMode = true;
                UpdateItemCount(AccountArray.Length);

                Comparison<Account> comparison = new Comparison<Account>(accountSort);
                accountList.Sort(comparison);
            }
            else
            {
                // No items downloaded, so count is 0.
                UpdateItemCount(0);
            }

            base.LoadComplete(e);
        }

        private int accountSort(Account left, Account right)
        {
            return left.Code.CompareTo(right.Code);
        }

        protected override void OnRetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            Account account = AccountArray[e.ItemIndex];

            e.Item = new ListViewItem(
                new string[] {
                    account.Code,
                    account.Title,
                    account.Balance.ToString("N") });

            e.Item.Tag = account;

            base.OnRetrieveVirtualItem(sender, e);
        }

        protected override void PerformEditorOpen(object tag)
        {
            Account account = (Account)tag;
            Program.LoadEditorAsync(new AccountEditor(Program, account));

            base.PerformEditorOpen(tag);
        }

        internal override BaseEditor GetEditor()
        {
            return new AccountEditor(Program);
        }

        private void createStatementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Account account = ItemsListView.FocusedItem.Tag as Account;
            StatementCreator creator = new StatementCreator(account);
            creator.ShowDialog(this);
        }
    }
}